package cn.zou.core.util.luncene;




import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;

import cn.zou.blog.entry.Article;

/**
 * 文章类和document之间的转换
 * @author zoujing
 *
 */
public class LunceneUtils {
	//文章类转换成document类
	public static Document BeanConversionDocument(Article article){
		Document document= new Document();
		StringField idField=new StringField("id", article.getId(), Store.YES);
		TextField allField=new TextField("all","1", Store.YES);
		TextField titleField=new TextField("title",article.getTitle(), Store.YES);
		TextField createField=new TextField("create",article.getCreate(), Store.YES);
		TextField typeField=new TextField("type",article.getType(), Store.YES);
		TextField imageUrlField=new TextField("imageUrl",article.getImageUrl(), Store.YES);
		TextField contantField=new TextField("contant",article.getContent(),Store.YES);
		TextField currnt=new TextField("currnt",String.valueOf(article.getCurrnt()),Store.YES);
		LongField modifyTime=new LongField("modifyTime",article.getModifyTime().getTime(),Store.YES);
		document.add(allField);
		document.add(typeField);
		document.add(createField);
		document.add(imageUrlField);
		document.add(contantField);
		document.add(titleField);
		document.add(idField);
		document.add(modifyTime);
		document.add(currnt);
		return document;
	}
	//document类转换成文章类
	public static Article DocumentConversionBean(Document doc){
		Article article=new Article();
		article.setId(doc.get("id"));
		article.setContent(doc.get("contant"));
		article.setTitle(doc.get("title"));
		article.setCreate(doc.get("create"));
		article.setImageUrl(doc.get("imageUrl"));
		article.setType(doc.get("type"));
		article.setCurrnt(new Long(doc.get("currnt")));
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String format = sdf.format(new Date(new Long(doc.get("modifyTime"))));
			article.setModifyTime(sdf.parse(format));
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return article;
	}
	public static void main(String[] args) {
		Article article=new Article();
		article.setId("1245");
		article.setTitle("我是你爸爸");
		article.setContent("<p>你算什么东西</p><p>你是什么东西</p>");
		try {
			article.setModifyTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2017-04-12 14:47:15"));
		} catch (ParseException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		article.setCurrnt(new Long(1));
		Document doc = BeanConversionDocument(article);
		IndexUtils.setPath("E:\\lucene");
		System.out.print(IndexUtils.indexSerchCount("是","title","contant"));
		//List<Article> indexSerch = IndexUtils.indexSerch("是", IndexUtils.getSort("corrnt"), 0, 2, "title","contant");
		//Article article2 = indexSerch.get(0);
		//System.out.println(article2.getContent());
		//System.out.print(article2.getTitle());
		//System.out.print(article2.getId());
	}
}
